//
//  RightMoreView.m
//  StationManager
//
//  Created by 泽华伟业 on 17/3/13.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "RightMoreView.h"
#import "DataHandel.h"
#import "OffLineMapViewController.h"
#import "WeatherViewController.h"
#import "LeiDaViewController.h"
#import "AboutUsViewController.h"
#import "MaintainSaveViewController.h"
#import "LogSearchViewController.h"
#import "SavedStationViewController.h"

@interface RightMoreView ()<UITableViewDelegate,UITableViewDataSource>
///列表
@property (nonatomic,strong) UITableView *tableView;

@end
@implementation RightMoreView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArray = @[@"查看已保存维护日志",@"查看已保存台站信息",@"更新本地台站信息",@"只显示维护台站信息",@"维护日志查询",@"天气预报",@"显示雷达图",@"周边搜索",@"关于我们"];
        [self tableView];
    }
    return self;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) style:UITableViewStylePlain];
        [self addSubview:_tableView];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self);
        }];
    }
    return _tableView;
}

#pragma mark -- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40*hl;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightMoreTableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rightMoreTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.textLabel setText:[_dataArray objectAtIndex:indexPath.row]];
    [cell.textLabel setFont:TEXT_FONT];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //收回本身视图
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.superview).offset(-40*8*hl);
        make.right.equalTo(self.superview);
        make.width.mas_equalTo(0*wl);
        make.height.mas_equalTo(0);
    }];

    if ([cell.textLabel.text isEqualToString:@"只显示维护台站信息"]) {
        [cell.textLabel setText:@"显示全部站点信息"];
        self.allOrBadStationBlcok(@"故障");
    }else if ([cell.textLabel.text isEqualToString:@"显示全部站点信息"]){
       [cell.textLabel setText:@"只显示维护台站信息"];
        self.allOrBadStationBlcok(@"全部");
    }
    else if ([cell.textLabel.text isEqualToString:@"查看已保存台站信息"]){
        SavedStationViewController * savedStationVC = [[SavedStationViewController alloc]init];
        [[self getController].navigationController pushViewController:savedStationVC animated:YES];
    }
    else if ([cell.textLabel.text isEqualToString:@"维护日志查询"]){
        LogSearchViewController * logVC = [[LogSearchViewController alloc]init];
        [[self getController].navigationController pushViewController:logVC animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"显示雷达图"]){
        LeiDaViewController *leidaVC = [[LeiDaViewController alloc]init];
        [[self getController].navigationController pushViewController:leidaVC animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"天气预报"]){
        WeatherViewController *weatherVC = [[WeatherViewController alloc]init];
        [[self getController].navigationController pushViewController:weatherVC animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"更新本地台站信息"]){
        self.updataStationBlock();
    }else if ([cell.textLabel.text isEqualToString:@"查看已保存维护日志"]){
        MaintainSaveViewController * saveVC = [[MaintainSaveViewController alloc]init];
        [[self getController].navigationController pushViewController:saveVC animated:YES];
    }else if ([cell.textLabel.text isEqualToString:@"关于我们"]){
        AboutUsViewController * aboutUsVC = [[AboutUsViewController alloc]init];
        [[self getController].navigationController pushViewController:aboutUsVC animated:YES];
    }
    else if ([cell.textLabel.text isEqualToString:@"周边搜索"]){
         self.searchNearByBlock();
    }

}

//获取视图的视图控制器
- (UIViewController *)getController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vc =  (UIViewController*)nextResponder;
            return vc;
        }
    }
    return nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
